package org.jeecg.modules.recharge.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.base.controller.JeecgController;
import org.jeecg.modules.discount.entity.Discount;
import org.jeecg.modules.discount.service.IDiscountService;
import org.jeecg.modules.recharge.entity.Balance;
import org.jeecg.modules.recharge.entity.OrderRecharge;
import org.jeecg.modules.recharge.service.IBalanceService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.recharge.service.IOrderRechargeService;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.voucher.entity.Voucher;
import org.jeecg.modules.voucher.service.IVoucherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 账户余额
 * @Author: jeecg-boot
 * @Date:   2024-06-18
 * @Version: V1.0
 */
@Api(tags="账户余额")
@RestController
@RequestMapping("/recharge/balance")
@Slf4j
public class BalanceController extends JeecgController<Balance, IBalanceService> {
	@Autowired
	private IBalanceService balanceService;
	@Autowired
	private IOrderRechargeService orderRechargeService;
	@Autowired
	private ISysUserService sysUserService;
	@Autowired
	private IDiscountService discountService;
	@Autowired
	private IVoucherService voucherService;

	 @ApiOperation(value="账户余额", notes="账户余额")
	 @GetMapping(value = "/mybalance")
	 public Result<Object> myBalance() {
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 Balance balance = balanceService.getOne(new LambdaQueryWrapper<Balance>().eq(Balance::getSysUserId,sysUser.getId()));
//		 List<Discount> discountList = discountService.list(new LambdaQueryWrapper<Discount>().eq(Discount::getStatus,"1"));
		 List<OrderRecharge> rechargeList = orderRechargeService.list(new LambdaQueryWrapper<OrderRecharge>().eq(OrderRecharge::getSysUserId,sysUser.getId()).eq(OrderRecharge::getPayStatus,"1"));
		 List<Voucher> voucherList = voucherService.list(new LambdaQueryWrapper<Voucher>().eq(Voucher::getSysUserId,sysUser.getId()));
		 Map<String, Object> params = new HashMap<>();
		 params.put("balance", balance);
//		 params.put("discountList", discountList);
		 params.put("rechargeList", rechargeList);
		 params.put("voucherList", voucherList);
		 return Result.OK(params);
	 }

	/**
	 * 分页列表查询
	 *
	 * @param balance
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "账户余额-分页列表查询")
	@ApiOperation(value="账户余额-分页列表查询", notes="账户余额-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<Balance>> queryPageList(Balance balance,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<Balance> queryWrapper = QueryGenerator.initQueryWrapper(balance, req.getParameterMap());
		Page<Balance> page = new Page<Balance>(pageNo, pageSize);
		IPage<Balance> pageList = balanceService.pageList(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param balance
	 * @return
	 */
	@AutoLog(value = "账户余额-添加")
	@ApiOperation(value="账户余额-添加", notes="账户余额-添加")
	//@RequiresPermissions("org.jeecg.modules:sr_balance:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody Balance balance) {
		balanceService.save(balance);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param balance
	 * @return
	 */
	@AutoLog(value = "账户余额-编辑")
	@ApiOperation(value="账户余额-编辑", notes="账户余额-编辑")
	//@RequiresPermissions("org.jeecg.modules:sr_balance:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody Balance balance) {
		balanceService.updateById(balance);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "账户余额-通过id删除")
	@ApiOperation(value="账户余额-通过id删除", notes="账户余额-通过id删除")
	//@RequiresPermissions("org.jeecg.modules:sr_balance:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		balanceService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "账户余额-批量删除")
	@ApiOperation(value="账户余额-批量删除", notes="账户余额-批量删除")
	//@RequiresPermissions("org.jeecg.modules:sr_balance:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.balanceService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "账户余额-通过id查询")
	@ApiOperation(value="账户余额-通过id查询", notes="账户余额-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<Balance> queryById(@RequestParam(name="id",required=true) String id) {
		Balance balance = balanceService.getById(id);
		if(balance==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(balance);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param balance
    */
    //@RequiresPermissions("org.jeecg.modules:sr_balance:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Balance balance) {
		String exportFieldsList = this.getExportFields(Balance.class,"balance:");
        return super.exportXls(request, balance, Balance.class, "账户余额");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("sr_balance:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Balance.class);
    }

}
